package com.sancai.fruit.utils;

import com.google.gson.GsonBuilder;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.sancai.fruit.api.Address;
import com.sancai.fruit.api.Apistore;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xiangyao on 2018/8/1.
 */

public class ApiManager {
    private static Apistore apistore;
    private static int TIME_OUT = 5000;


    public static Apistore getApistore() {
        synchronized (ApiManager.class) {
            if (apistore == null) {
                OkHttpClient okHttpClient = new OkHttpClient.Builder()
                        .connectTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                        .addInterceptor(new LogInterceptor(false))
                        .build();
                //创建retrofit实例对象
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Address.BASEURL)      //设置服务器主机,以"/"结尾
                        .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().enableComplexMapKeySerialization().create()))     //配置gson解析器
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())   //响应回调
                        .client(okHttpClient)
                        .build();
                apistore = retrofit.create(Apistore.class);
            }
        }
        return apistore;
    }
}
